$(function() {
	var _this = '';
	var flag = 0;
	var n = [];
	var uploadPath = $("#ctx").val() + "/resources/upload/";
	$("#attach").uploadify(
			{
				swf : $("#ctx").val() + "/resources/uploadify/uploadify.swf",
				uploader : $("#ctx").val() + "/admin/topic/upload.do",
				fileTypeExts : "*.jpg;*.png;*.bmp",
				formData : {
					"sid" : $("#sid").val(),
					"attTypeId" : String($('.proType').val())
				},
				fileObjName : "attach",
				auto : false,
				onUploadSuccess : function(file, data, response) {
					var ao = $.parseJSON(data);
					var suc = $.ajaxCheck(ao);
					if (suc) {
						var node = createAttachNode(ao.obj);
						$("#tbody").append(node);
						UE.getEditor(ao.obj.id + 'm', {
							toolbars : [ [ 'fullscreen', 'undo', 'redo',
									'bold', 'fontsize', 'justifyleft',
									'justifyright', 'justifycenter',
									'justifyjustify', 'forecolor' ] ],
							autoHeightEnabled : false,
							autoFloatEnabled : true
						});
						UE.getEditor(ao.obj.id + 'p', {
							toolbars : [ [ 'fullscreen', 'undo', 'redo',
									'bold', 'fontsize', 'justifyleft',
									'justifyright', 'justifycenter',
									'justifyjustify', 'forecolor',
									'simpleupload' ] ],
							autoHeightEnabled : false,
							autoFloatEnabled : true
						});
					}
				}
			});
	function createAttachNode(attach) {
		n.push(attach.newName);
		var node = "<tr>";
		node += "<td><img src='" + uploadPath + "thumbnail/" + attach.newName
				+ "'/><div>首页显示？<input type='checkbox' id='checkbox' idv='"
				+ attach.id + "'><div></td>";
		node += "<td style='width:100px;'>"
				+ $('.proType option:selected').text() + "</td>"
		node += "<td><textarea id='" + attach.id + "n' class='proNameText'>"
				+ attach.oldName + "</textarea></td>"
		node += "<td style='border: 1px solid black; '><textarea id='"
				+ attach.id
				+ "m' style='width:350px;height:200px;'></textarea></td>";
		node += "<td style='border: 1px solid black; '><textarea id='"
				+ attach.id
				+ "p' style='width:350px;height:200px;'></textarea></td>";
		node += "<td><span id='"
				+ attach.id
				+ "s' style='color:red;'>未保存</span><div class='confirm bfont' proName='"
				+ attach.id + "n' proMessage='" + attach.id + "m' propar='"
				+ attach.id + "p' id='" + attach.id + "' isSave='" + attach.id
				+ "s'>保存</div>" + "<div proName='" + attach.id
				+ "n' proMessage='" + attach.id
				+ "m' class='edit bfont' notSave='" + attach.id
				+ "s'  propar='" + attach.id + "p'>修改</div>"
				+ "<div class='bfont delete' id='" + attach.id
				+ "'>删除</div></td>"
		if (attach.isImg) {
		} else {
		}
		node += "</tr>";
		return node;
	}
	$("#ok_attach").on("click", "#checkbox", function() {
		var id = $(this).attr('idv');
		if ($(this).is(':checked')) {
			dwrService.updateIndexImg(id, 1);
		} else {
			dwrService.updateIndexImg(id, 0);
		}
	});
	$("#ok_attach").on("click", ".delete", function() {
		_this = this;
		$('#mes').html('确定要删除吗?');
		$('.con-mes').fadeIn();
		$('.cover').css('display', 'block');
	});
	$('#con-mes').click(function() {
		if (flag == 0) {
			var id = $(_this).attr('id');
			$(_this).parent("td").parent("tr").empty();
			$('.con-mes').fadeOut();
			$('.cover').css('display', 'none');
			dwrService.deleteAttach(id);
			_this = '';
		} else {
			window.opener.location.reload(true);
			window.close();
		}
	});
	$('#del-mes').click(function() {
		$('.con-mes').fadeOut();
		$('.cover').css('display', 'none');
		flag = 0;
	});
	$('.complete').click(function() {
		$('#mes').html('该操作不可逆，请确定保存完毕~');
		flag = 1;
		$('.con-mes').fadeIn();
		$('.cover').css('display', 'block');
	});
	$("#ok_attach").on("click", ".edit", function() {
		UE.getEditor($(this).attr('proMessage')).setEnabled();
		UE.getEditor($(this).attr('propar')).setEnabled();
		$('#' + $(this).attr('proName')).attr('disabled', false);
		$('#' + $(this).attr('notSave')).html('未保存').css('color', 'red');
	});
	$("#ok_attach").on("click", ".confirm", function() {
		var proName = $('#' + $(this).attr('proName')).val();
		var id = $(this).attr('id');
		var proMessage = UE.getEditor($(this).attr('proMessage')).getContent();
		var propar = UE.getEditor($(this).attr('propar')).getContent();
		dwrService.updatePro(proName, proMessage, propar, id);
		UE.getEditor($(this).attr('proMessage')).setDisabled('fullscreen');
		UE.getEditor($(this).attr('propar')).setDisabled('fullscreen');
		$('#' + $(this).attr('proName')).attr('disabled', true);
		$('#' + $(this).attr('isSave')).html('已保存').css('color', 'green');
	});
	$("#upload").click(function() {
		$("#attach").uploadify("upload", "*");
	});
	$(".proType").change(
			function() {
				$("#attach").uploadify(
						{
							swf : $("#ctx").val()
									+ "/resources/uploadify/uploadify.swf",
							uploader : $("#ctx").val() + "/admin/topic/upload.do",
							fileTypeExts : "*.jpg;*.png;*.bmp",
							formData : {
								"sid" : $("#sid").val(),
								"attTypeId" : String($('.proType').val())
							},
							fileObjName : "attach",
							auto : false,
							onUploadSuccess : function(file, data, response) {
								var ao = $.parseJSON(data);
								var suc = $.ajaxCheck(ao);
								if (suc) {
									var node = createAttachNode(ao.obj);
									$("#tbody").append(node);
									UE.getEditor(ao.obj.id + 'm',
											{
												toolbars : [ [ 'fullscreen',
														'undo', 'redo', 'bold',
														'fontsize',
														'justifyleft',
														'justifyright',
														'justifycenter',
														'justifyjustify',
														'forecolor' ] ],
												autoHeightEnabled : false,
												autoFloatEnabled : true
											});
									UE.getEditor(ao.obj.id + 'p', {
										toolbars : [ [ 'fullscreen', 'undo',
												'redo', 'bold', 'fontsize',
												'justifyleft', 'justifyright',
												'justifycenter',
												'justifyjustify', 'forecolor',
												'simpleupload' ] ],
										autoHeightEnabled : false,
										autoFloatEnabled : true
									});
								}
							}
						});
			});
})